<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }
        #app{
            margin: 0 auto;
        }
        table,
        tr,
        td {
            border: 1px solid red;
            border-collapse: collapse;
        }

        td {
            padding: 10px 20px;
        }
    </style>
</head>

<body>
    <div id="app">
        <table>
            <tr>
                <td>ID</td>
                <td>标题</td>
                <td>价格</td>
                <td>数量</td>
                <td>小计</td>
            </tr>
            <tr v-for="c in cart" :key="c.id">
                <td>{{c.id}}</td>
                <td>{{c.title}}</td>
                <td>{{c.price}}</td>
                <td>
                    <button @click="minus(c)">-</button>
                    {{c.count}}
                    <button @click="add(c)">+</button>
                </td>
                <td>{{c.count * c.price}}</td>
            </tr>
        </table>
        <h3>
            总价：{{total}}
        </h3>
    </div>
    <script>
        const VM = new Vue({
            el: '#app',
            data() {
                return {
                    cart: [
                        { id: 1, title: '手机', price: 1999, count: 1 },
                        { id: 2, title: '电脑', price: 9992, count: 1 },
                        { id: 3, title: '衣服', price: 29, count: 1 },
                        { id: 4, title: '洗衣机', price: 5321, count: 1 },
                        { id: 5, title: '灯', price: 19, count: 1 },
                        { id: 6, title: '球杆', price: 2999, count: 1 },
                    ]
                }
            },
            methods: {
                minus(item) {
                    if (item.count > 1) {
                        item.count--
                    }
                },
                add(item) {
                    item.count++;
                }
            },
            computed: {
                total: {
                    get() {
                        return this.cart.reduce((pre, item) => pre + item.count * item.price, 0);
                    }
                }
            }
        });
    </script>
</body>

</html>